/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "EvolutiveLib.h"
#include <stdlib.h>
#include <fstream>

#ifdef _WIN32
#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

#endif

#ifdef USE_OPENCV
void Evolutive::SaveImageData(IplImage *Image,string FileName)
{
	ofstream fout(FileName.data());
	int i,j;
	int *pImgDataI;
	unsigned char *pImgDataUI;
	char Buffer[32];
	for(i=0;i<Image->height;i++)
	{
		if(Image->depth==IPL_DEPTH_8U)
			pImgDataUI=(unsigned char*)&(Image->imageData[i*Image->widthStep]);
		else
			pImgDataI=(int*)&(Image->imageData[i*Image->widthStep]);

		for(j=0;j<Image->width;j++)
		{
			if(Image->depth==IPL_DEPTH_8U)
				sprintf(Buffer,"%5d ",pImgDataUI[j]);
			else
				sprintf(Buffer,"%5d ",pImgDataI[j]);
			fout << Buffer;
		}
		fout << endl;
	}
	fout.close();
}
#endif//USE_OPENCV

Evolutive::CEvolutiveLibException::CEvolutiveLibException(void) : m_FileLine(-1)//exception(),m_FileLine(-1)
{
}

Evolutive::CEvolutiveLibException::CEvolutiveLibException(string Desc) : m_FileLine(-1)//exception(Desc.data()),m_FileLine(-1)
{	
	m_ErrorDesc=Desc;	
}

Evolutive::CEvolutiveLibException::CEvolutiveLibException(string Desc,string File,int Line) : m_FileLine(-1)//exception(Desc.data()),m_FileLine(-1)
{
	m_File=File;
	m_FileLine=Line;
	m_ErrorDesc=Desc;	
}

Evolutive::CEvolutiveLibException::CEvolutiveLibException(string Desc,string File,int Line,string Function) : m_FileLine(-1)//exception(Desc.data()),m_FileLine(-1)
{
	m_File=File;
	m_FileLine=Line;
	m_ErrorDesc=Desc;	
	m_Function=Function;
}

Evolutive::CEvolutiveLibException::~CEvolutiveLibException(void)
{
}

string Evolutive::CEvolutiveLibException::ToString(void)
{
	string Output;
	char Buffer[128];

	Output="EvolutiveLib Error\n";
	//! Add the error description
	if(!m_ErrorDesc.empty())
	{
		Output+="\tDesc: ";
		Output+=m_ErrorDesc;
		Output+="\n";
	}
	//! Add the file information
	if(!m_File.empty())
	{
		Output+="\tFile: ";
		Output+=m_File;
		Output+="\n";
	}
	//! Add the line information
	if(m_FileLine>=0)
	{
		Output+="\tLine: ";
		//_itoa_s(m_FileLine,Buffer,128,10);
		sprintf(Buffer,"%d",m_FileLine);
		Output+=Buffer;
		Output+="\n";
	}
	//! Add the function information
	if(!m_Function.empty())
	{
		Output+="\tFunction: ";
		Output+=m_Function;
		Output+="\n";
	}

	return Output;
}

const char *Evolutive::CEvolutiveLibException::what(void)
{
	string S;

	S=ToString();

	return S.data();
}

bool Evolutive::CEvolutiveLibException::IsError(string Desc)
{
	if(m_ErrorDesc.empty() && Desc.empty())
		return true;
	if(m_ErrorDesc.empty())
		return false;
	return m_ErrorDesc.compare(Desc);

}